﻿using Newtonsoft.Json;
using System.Collections.Generic;

namespace TomNet.Data
{
    /// <summary>
    /// 分页信息输出
    /// </summary>
    public class PageData<T>
    {
        public PageData()
        {
            List = new List<T>();
        }

        /// <summary>
        /// 数据总数
        /// </summary>
        public long TotalCount { get; set; } = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        public long TotalPage { get => GetTotalPage(TotalCount, PageSize); }

        /// <summary>
        /// 每页多少条数据
        /// </summary>
        public long PageSize { get; set; } = 0;

        /// <summary>
        /// 页码
        /// </summary>
        public long PageNumber { get; set; } = 1;

        /// <summary>
        /// 数据
        /// </summary>
        [JsonProperty(PropertyName = "body")]
        public IList<T> List { get; set; }

        /// <summary>
        /// 计算总页数
        /// </summary>
        /// <param name="total">总条数</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns>总页数</returns>
        public long GetTotalPage(long total, long pageSize)
        {
            return total / pageSize + (total % pageSize == 0 ? 0 : 1);
        }

        /// <summary>
        /// 获取页码
        /// </summary>
        /// <param name="pageNumber">用户输入页码</param>
        /// <returns>页码</returns>
        public long GetPageNumber(int pageNumber)
        {
            return TotalPage < pageNumber ? TotalPage : pageNumber;
        }

    }
}
